﻿Public Class clsSimTime
    Private Shared _lastTimes As New Dictionary(Of String, DateTime)
    Private Shared _simSpeed As Single = 1.0


    Private Sub New()
    End Sub

    Public Shared Property SimulationSpeed As Single
        Get
            Return _simSpeed
        End Get
        Set(ByVal value As Single)
            _simSpeed = value
        End Set
    End Property

    Public Shared Sub ClearTimeControl()
        _lastTimes.Clear()
    End Sub

    Public Shared Function AddTimeControl(ByVal caller As String) As String
        If _lastTimes.ContainsKey(caller) Then
            Dim i As Integer = 1
            Do While _lastTimes.ContainsKey(caller + i.ToString)
                i += 1
            Loop
            _lastTimes.Add(caller + i.ToString, DateTime.Now)
            Return (caller + i.ToString)
        End If
        _lastTimes.Add(caller, DateTime.Now)
        Return caller
    End Function
    Public Shared Function GetTimeDifference(ByVal caller As String) As Double
        If _lastTimes.ContainsKey(caller) Then
            Dim last As DateTime = _lastTimes(caller)
            _lastTimes(caller) = DateTime.Now
            Return (TimeSpan.FromTicks(_lastTimes(caller).Ticks - last.Ticks).TotalSeconds * _simSpeed)
        Else
            '_lastTimes.Add(caller, DateTime.Now)
            Return 0
        End If
    End Function
    Public Shared Sub RemoveTimeControl(ByVal caller As String)
        If _lastTimes.ContainsKey(caller) Then
            _lastTimes.Remove(caller)
        End If
    End Sub

    Public Shared ReadOnly Property TimeControlCount As Integer
        Get
            Return _lastTimes.Count
        End Get
    End Property
End Class
